//
//  TYEditCollectionViewCell.m
//  Somago
//
//  Created by 谢航宇_WORK on 2021/11/4.
//  Copyright © 2021 juniu. All rights reserved.
//

#import "TYEditCollectionViewCell.h"
#import "Masonry.h"
@interface TYEditHiddenView : UIView
//侧滑大小
@property (nonatomic, weak) TYEditCollectionViewCell * containView;
@end

@interface TYEditCollectionViewCell()
@end

@implementation TYEditCollectionViewCell
- (UIView *)containView {
    if (!_containView) {
        _containView = [[UIView alloc]init];
        self.layer.masksToBounds = YES;
        [self addSubview:_containView];
        __weak typeof(self) weakSelf = self;
        [_containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(weakSelf);
        }];
     }
    return _containView;
}

- (UIView *)editView {
    if (!_editView) {
        _editView = [[UIView alloc]init];
        _editView.backgroundColor = [UIColor redColor];
        [self addSubview:_editView];
        __weak typeof(self) weakSelf = self;
        [_editView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.top.mas_equalTo(weakSelf);
            make.left.mas_equalTo(weakSelf.containView.mas_right).offset(0);
            make.width.mas_equalTo(0);
        }];
    }
    return _editView;
}

- (void)setRightSpace:(CGFloat)rightSpace {
    _rightSpace = rightSpace;
    [self.editView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(rightSpace);
    }];
}
 

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    __weak typeof(self) weakSelf = self;
    if (_isEdit) {
        TYEditHiddenView * editHiddenView = [[TYEditHiddenView alloc]initWithFrame:[[UIApplication sharedApplication]delegate].window.bounds];
        editHiddenView.containView = self;
        [[[UIApplication sharedApplication]delegate].window addSubview:editHiddenView];
        [editHiddenView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo([[UIApplication sharedApplication]delegate].window);
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.containView.transform = CGAffineTransformMakeTranslation(- weakSelf.rightSpace, 0);
            weakSelf.editView.transform = CGAffineTransformMakeTranslation(- weakSelf.rightSpace, 0);
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.containView.transform = CGAffineTransformMakeTranslation(0, 0);
            weakSelf.editView.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
    }
}
@end


@implementation TYEditHiddenView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    __weak typeof(self) weakSelf = self;
    static BOOL isCalculate = NO;
    if (isCalculate) {
        return nil;
    }
    isCalculate = YES;
    UIView * view = [[[UIApplication sharedApplication]delegate].window hitTest:point withEvent:event];
    isCalculate = NO;
    if ([self.containView.editView.subviews containsObject:view] || view == self.containView.editView) {
        return nil;
    }
    self.containView.isEdit = NO;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC));
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        [weakSelf removeFromSuperview];
    }); 
    return self;
}
@end
